package com.ezgzone.webcrawler;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.sqrq.DocumentX;
import com.sqrq.JsoupX;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileAppender;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;

public class ImageDownLmjxType {

	static String templateSql = "INSERT INTO `wm_machine_type`(`id`, `pid`, `path`, `name`, `level`, `sort`, `pic`, `created_at`, `updated_at`) "
			+ "VALUES ('{}','{}','{}','{}',{},'{}','{}','2020-12-15 00:00:00','2020-12-15 00:00:00');";

	public static void main(String[] args) throws IOException {
		File imageSavedDir = FileUtil.file("C:\\temp\\type_img");
		FileUtil.mkdir(imageSavedDir);

		File fileSql = FileUtil.file(imageSavedDir, "wm_machine_type.sql");
		FileAppender appender = new FileAppender(fileSql, 16, true);

		String url = "https://zj.lmjx.net/model/";
		DocumentX doc = JsoupX.connect(url).timeout(50000).get();
		String title = doc.doc().title();
		System.out.println("title: " + title);

		// Elements els = doc.byTag("div").byAttr("class", "promms").currentEles();
		Elements els = doc.byTag("div").byAttr("class", "prommscon").currentEles();
		System.out.println("Getting all prommscon[" + els.size() + "]...");
		// System.out.println(els.html());
		Element prommscon = els.first();
		Elements items = prommscon.children();

		ArrayList<String> sqlList1 = new ArrayList<String>(), sqlList2 = new ArrayList<String>(),
				sqlList3 = new ArrayList<String>();
		System.out.println("Getting all items[" + items.size() + "]...");
		for (int i = 0; i < items.size(); i++) {
			Element item = items.get(i);
			Element ilabel = item.getElementsByClass("ilabel").first();
			String href1 = ilabel.getElementsByTag("a").first().absUrl("href");
			String temp1[] = href1.split("/");
			String tag1 = temp1[temp1.length - 1];
			String name1 = ilabel.getElementsByTag("a").first().getElementsByTag("span").text();
			String level1 = "1";
			System.out.println("item -> level:" + level1 + " sort:" + (i + 1) + " name:" + name1 + ", href:" + href1
					+ ", tag:" + tag1);
			if ("配套件及服务".equals(name1))
				continue;

			// sql
			// `id`, `pid`, `path`, `name`, `level`, `sort`, `pic`
			int id1 = i + 1;
			String path1 = id1 + "";
			String strSql1 = StrUtil.format(templateSql, id1, 0, path1, name1, level1, (i + 1), tag1);
			sqlList1.add(strSql1);

			Element itemin = item.getElementsByClass("itemin").first();
			Elements its = itemin.getElementsByClass("it");
			for (int j = 0; j < its.size(); j++) {
				Element it = its.get(j);
				String href2 = it.absUrl("href");
				String name2 = it.text();
				String level2 = "2";
				temp1 = href2.split("/");
				String tag2 = temp1[temp1.length - 1];
				System.out.println("item ->   level:" + level2 + " sort:" + (j + 1) + " name:" + name2 + ", href:"
						+ href2 + ", tag:" + tag2);

				// sql
				// `id`, `pid`, `path`, `name`, `level`, `sort`, `pic`
				int id2 = id1 * 100 + j + 1;
				String path2 = id1 + "-" + id2;
				String strSql2 = StrUtil.format(templateSql, id2, id1, path2, name2, level2, (i + 1), tag2);
				sqlList2.add(strSql2);

				crawlmms(href2, id2, path2, name2, sqlList3);
				// crawlmms("https://zj.lmjx.net/model/wajueji/", 101, "1-101", "挖掘机",
				// sqlList3);
			}

		}

//		File fileSql = FileUtil.file(imageSavedDir, "wm_machine_brand.sql");
//		String templateSql = "INSERT INTO `wm_machine_brand`(`brand_id`, `brand_name`, `manufacturer`, `pinyin`, `sort`, `pic`, `created_at`, `updated_at`) "
//				+ "VALUES ('{}','{}','{}','{}',0,'{}','2020-12-15 00:00:00','2020-12-15 00:00:00');";
//		FileAppender appender = new FileAppender(fileSql, 16, true);
//		int i = 0;
//		for (Element eImg : els) {
//			i++;
//			// System.out.println(eImg.attr("title") + "->" + eImg.attr("abs:href"));
//			String imgSrc = eImg.getElementsByTag("img").get(0).absUrl("lz_src");
//			Element a = eImg.nextElementSibling();
//			String manufacturer = a.attr("title");
//			String temp1[] = a.attr("href").split("/");
//			String temp2[] = temp1[temp1.length - 1].split("_");
//			String pinyin = temp2[temp2.length - 1];
//			String brandName = a.text();
//			String strPrint = StrUtil.format("brandName:{},pinyin:{},manufacturer:{},imgSrc:{}", brandName, pinyin,
//					manufacturer, imgSrc);
//			System.out.println(strPrint);
//			File destFile = FileUtil.file(imageSavedDir, pinyin + ".jpg");
//			long size = HttpUtil.downloadFile(imgSrc, destFile);
//			// long size = HttpUtil.downloadFile(fileUrl, FileUtil.file("e:/"));
//			System.out.println("Download size: " + size);
//
//			// sql
//			String strSql = StrUtil.format(templateSql, i, brandName, manufacturer, pinyin,
//					"attachment/machine/brand/" + pinyin + ".jpg");
//			appender.append(strSql);
//			appender.flush();
//			appender.toString();
//		}

		// crawlmms("https://zj.lmjx.net/model/wajueji/", 101, "1-101", "挖掘机",
		// sqlList3);

		// crawlmms("https://zj.lmjx.net/model/tanpuji/", "1", "1-3", "沥青摊铺机");

		for (String s : sqlList1) {
			appender.append(s);
		}
		for (String s : sqlList2) {
			appender.append(s);
		}
		for (String s : sqlList3) {
			appender.append(s);
		}
		appender.flush();
		appender.toString();
	}

	private static void crawlmms(String url, int parentId, String parentPath, String parentName,
			ArrayList<String> sqlList) throws IOException {
		DocumentX doc = JsoupX.connect(url).timeout(50000).get();
		String title = doc.doc().title();
		System.out.println("title: " + title);

		Elements els = doc.byTag("div").byAttr("class", "select_mms").currentEles();
		System.out.println("Getting all select_mms[" + els.size() + "]...");
		// System.out.println(els.html());
		if (els.isEmpty()) {
			System.out.println("该地址没有三级数据[" + url + "]!!!");
			return;
		}

		Element selectmms = els.first();
		Element mmsxh = selectmms.getElementsByClass("mmsxh").first();
		Elements items = mmsxh.children();

		System.out.println("Getting all items[" + items.size() + "]...");
		for (int i = 0; i < items.size(); i++) {
			Element item = items.get(i);
			String ihref = item.absUrl("href");
			String iname = item.text();
			String temp1[] = ihref.split("/");
			String itag = temp1[temp1.length - 1];
			String name = StrUtil.removeAll(iname, parentName);
			name = StrUtil.subBefore(name, "(", false);
			String level = "3";
			System.out.println("item -> level:" + level + " sort:" + (i + 1) + " name:" + name + ", href:" + ihref
					+ ", idtag:" + itag);
			if (StrUtil.isEmpty(name))
				continue;

			// sql
			// `id`, `pid`, `path`, `name`, `level`, `sort`, `pic`
			int id3 = parentId * 1000 + i + 1;
			String path3 = parentPath + "-" + id3;
			String strSql3 = StrUtil.format(templateSql, id3, parentId, path3, name, level, (i + 1), itag);
			sqlList.add(strSql3);
		}
		// sql
		// `id`, `pid`, `path`, `name`, `level`, `sort`, `pic`
		int id3 = parentId * 1000 + items.size() + 1;
		String path3 = parentPath + "-" + id3;
		String strSql3 = StrUtil.format(templateSql, id3, parentId, path3, "其他", "3", (items.size() + 1), "其他");
		sqlList.add(strSql3);
	}
}
